import 'package:flutter/material.dart';
import 'package:flutter_widget/src/config/color_define.dart';
import 'package:flutter_widget/src/widget/text/text_field_format.dart';

///圆角输入框
class RoundTextField extends StatelessWidget {
  final TextEditingController? textController;
  final double? radius; //圆角大小
  final double height; //输入框高度
  final TextInputAction inputAction; //键盘事件『完成、搜索』
  final Function? finishCallback; //完成回调
  final Function? changedCallback; //修改回调
  final String placeholderText; //提示文本
  final EdgeInsetsGeometry roundPadding; //圆角内部边距
  final double fontSize; //字体大小
  final Widget? iconWidget; //左侧图标
  final Widget? rightIcon; //右侧小图标组件
  final Color? backgroundColor; //背景色
  final Color? cursorColor; //光标颜色
  final Color? textColor; //文字颜色
  final Color? placeholderColor; //提示文字颜色
  final FocusNode? focus; //焦点
  final bool enabled; //用户输入使能
  final bool autoFocus; //自动对焦
  final TextAlign? textAlign;
  final int? maxLines;

  const RoundTextField({
    this.finishCallback,
    this.placeholderText = "",
    this.radius,
    this.roundPadding = const EdgeInsets.fromLTRB(6, 0.0, 6, 0.0),
    this.fontSize = 12.0,
    this.height = 36,
    this.changedCallback,
    this.enabled = true,
    this.autoFocus = false,
    this.inputAction = TextInputAction.done,
    this.iconWidget,
    this.rightIcon,
    this.backgroundColor,
    this.cursorColor,
    this.textColor,
    this.placeholderColor,
    FocusNode? focusNode,
    TextEditingController? controller,
    this.textAlign,
    this.maxLines,
  })  : textController = controller,
        focus = focusNode,
        super();

  @override
  Widget build(BuildContext context) {
    return Container(
      // 修饰搜索框
      padding: roundPadding,
      alignment: Alignment.center,
      height: height,
      decoration: BoxDecoration(
        color: backgroundColor ?? FWColorPicker.grayLine,
        borderRadius: BorderRadius.all(
          Radius.circular(radius ?? height / 2),
        ),
      ),
      child: buildTextField(context),
    );
  }

  Widget buildTextField(BuildContext context) {
    // theme设置局部主题
    return Center(
      child: Theme(
        data: ThemeData(primaryColor: Colors.grey),
        child: TextFieldFormatWidget(
          focusNode: focus,
          autofocus: autoFocus,
          // 光标颜色
          cursorColor: cursorColor ?? FWColorPicker.colorFFDAB27E,
          controller: textController,
          maxLines: maxLines ?? 1,
          enabled: enabled,
          //监听文字改变
          onChanged: (changedCallback != null)
              ? (val) {
                  changedCallback!(val);
                }
              : null,
//          //点击键盘的动作按钮时的回调，没有参数
//          onEditingComplete: () {
//          },
          //点击确认
          onSubmitted: finishCallback != null
              ? (val) {
                  debugPrint("点击确认 ：$val");
                  finishCallback!(val);
                }
              : null,
          textInputAction: inputAction,
          textAlign: textAlign ?? TextAlign.left,
          textAlignVertical: TextAlignVertical.center,
          decoration: InputDecoration(
            border: InputBorder.none,
            // 去掉下滑线
            enabledBorder: const OutlineInputBorder(
              borderSide: BorderSide(
                color: Colors.transparent, //边线颜色
                width: 0,
              ),
            ),
            focusedBorder: const OutlineInputBorder(
              borderSide: BorderSide(
                color: Colors.transparent, //边框颜色
                width: 0,
              ),
            ),
            contentPadding: enabled == false
                ? EdgeInsets.fromLTRB(0, 0, 0, height / 2 + 1)
                : EdgeInsets.zero,
            prefixIconConstraints: const BoxConstraints(
                //添加内部图标之后，图标和文字会有间距，实现这个方法，不用写任何参数即可解决
                ),
            prefixIcon: iconWidget,
            suffixIcon: rightIcon,
            filled: false,
            hintText: placeholderText,
            //提示文本
            hintStyle: TextStyle(
              fontSize: fontSize,
              color: FWColorPicker.colorFF999999,
              height: 1.2,
              textBaseline: TextBaseline.ideographic,
              fontWeight: FontWeight.w400,
            ),
          ),
          style: TextStyle(
            fontSize: fontSize,
            color: textColor ?? Colors.black,
            height: 1.2,
            textBaseline: TextBaseline.ideographic,
            fontWeight: FontWeight.w400,
          ),
        ),
      ),
    );
  }
}